package com.example.hxpc.guidancemanagement.tools;

import android.app.Activity;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;
import android.widget.TextView;



/**
 * Created by huhaichao on 2017/6/26.
 */

public class TimeCount extends CountDownTimer {
    private Activity mActivity;
    private TextView btn = null;
    private ImageView scanImg = null;
    int style = -1;

    public TimeCount(Activity mActivity, long millisInFuture, long countDownInterval, TextView btn) {
        super(millisInFuture, countDownInterval);
        this.mActivity = mActivity;
        this.btn = btn;
    }

    public TimeCount(long millisInFuture, long countDownInterval, ImageView scanImg) {
        super(millisInFuture, countDownInterval);
        this.scanImg = scanImg;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (btn != null) {
            btn.setClickable(false);//设置不能点击
            btn.setText(millisUntilFinished / 1000 + "秒后重发");//设置倒计时时间
            Spannable span = new SpannableString(btn.getText().toString());//获取按钮的文字
            span.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//讲倒计时时间显示为红色
            btn.setText(span);
        }
        if (scanImg != null) {
            //scanImg.setClickable(false);
        }
    }

    @Override
    public void onFinish() {
        if (btn != null) {
            btn.setText("重新获取");
            btn.setClickable(true);//重新获得点击
        }
        if (scanImg != null) {
            scanImg.setClickable(true);//重新获得点击
        }
    }
}
